<template>
    <div class="content">
        <div class="query" v-if="showQuery">
            <el-form :inline="true" :model="form" label-width="auto">
                <el-form-item label="操作人：">
                    <el-input clearable v-model="form.operatorNm" style="width: 200px" placeholder="请输入操作人查询" />
                </el-form-item>
                <el-form-item label="请求路径：">
                    <el-input clearable v-model="form.requestUrl" style="width: 200px" placeholder="请输入请求路径查询" />
                </el-form-item>
                <el-form-item label="错误时间：">
                    <el-date-picker v-model="value" type="datetimerange" :shortcuts="shortcuts" range-separator="至"
                        start-placeholder="开始时间" end-placeholder="结束时间" @change="changeTime" style="width: 300px" />
                </el-form-item>
                <el-form-item label-width="200">
                    <el-button type="primary" @click="getData" :icon="Search">查询</el-button>
                    <el-button @click="reset" :icon="Refresh">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="fun">
            <div class="left">
                <el-popconfirm title="请确认执行批量删除操作?" confirm-button-text="确认" cancel-button-text="取消"
                    @confirm="deleteData('batch')">
                    <template #reference>
                        <el-button type="danger" :disabled="ids.length == 0" plain :icon="Delete">批量删除</el-button>
                    </template>
                </el-popconfirm>
            </div>
            <div class="right">
                <el-tooltip :content="showQuery ? '隐藏搜索栏' : '展示搜索栏'" placement="top">
                    <el-button :icon="TurnOff" circle @click="showQuery = false" v-if="showQuery" />
                    <el-button :icon="Open" circle @click="showQuery = true" v-else />
                </el-tooltip>
                <el-tooltip content="刷新" placement="top">
                    <el-button :icon="Refresh" circle @click="getData()" />
                </el-tooltip>
            </div>
        </div>
        <div class="table">
            <el-table empty-text="暂无数据" :show-overflow-tooltip="true" :data="tableData" v-loading="loading"
                element-loading-text="数据加载中..."
                :header-cell-style="{ background: 'var(--table-title-color)', color: 'var(--table-title-font-color)' }"
                @selection-change="handleSelectionChange" border>
                <el-table-column type="selection" width="55" />
                <el-table-column prop="requestUrl" label="请求路径" />
                <el-table-column prop="params" label="请求参数" />
                <el-table-column prop="errorType" label="异常类型" />
                <el-table-column prop="username" label="操作人" />
                <el-table-column prop="createTime" label="错误时间" />
                <el-table-column :fixed="$isMobile() ? false : 'right'" label="操作" width="200" column-align="center">
                    <template #default="scope">
                        <el-button text type="primary" size="small" @click="toDetail(scope.row.id)"
                            :icon="ChatLineSquare">详情</el-button>
                        <el-popconfirm title="请确认执行删除操作?" confirm-button-text="确认" cancel-button-text="取消"
                            @confirm="deleteData(scope.row.id)">
                            <template #reference>
                                <el-button link type="primary" size="small" :icon="Delete">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-config-provider>
            <el-pagination v-model:current-page="page.currentPage" v-model:page-size="page.pageSize"
                :page-sizes="[10, 20, 30, 40]" :pager-count="$isMobile() ? 5 : 7"
                :layout="$isMobile() ? 'prev,pager,next' : 'total, sizes, prev, pager, next, jumper'"
                :background="!$isMobile()" :total="page.total" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" />
        </el-config-provider>
        <logDetail ref="detail"></logDetail>
    </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { Delete, ChatLineSquare, Search, TurnOff, Open, Refresh } from '@element-plus/icons-vue'
import api from "@/api"
import { ElMessage } from 'element-plus'
import moment from 'moment'
import logDetail from "./logDetail.vue";

const shortcuts = [
    {
        text: '最近一周',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setDate(start.getDate() - 7)
            return [start, end]
        },
    },
    {
        text: '最近一月',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setMonth(start.getMonth() - 1)
            return [start, end]
        },
    },
    {
        text: '最近三月',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setMonth(start.getMonth() - 3)
            return [start, end]
        },
    },
]
const value = ref()
const loading = ref(false)
const form = ref({})
const page = ref({ pageSize: 10, pageNum: 1, currentPage: 0, total: 0 })
const tableData = ref([])
const ids = ref([])
const detail = ref()
const showQuery = ref(true)

const changeTime = (event) => {
    if (event) {
        const startTime = moment(event[0]).format('yyyy-MM-DD HH:mm:ss')
        form.value.startTime = startTime
        const endTime = moment(event[1]).format('yyyy-MM-DD HH:mm:ss')
        form.value.endTime = endTime
    } else {
        form.value.startTime = ''
        form.value.endTime = ''
    }
    console.log(form.value)
}

//获取数据
const getData = async () => {
    loading.value = true
    let params = { pageNum: page.value.pageNum, pageSize: page.value.pageSize, ...form.value }
    const res = await api.get('/system/errorLogs/pageList', params)
    if (res.data.code === 200) {
        tableData.value = res.data.data.records
        page.value.currentPage = res.data.data.current
        page.value.total = res.data.data.total
        loading.value = false
    } else {
        ElMessage.error(res.data.msg)
    }
}
//切换页
const handleSizeChange = (val) => {
    page.value.pageSize = val
    getData()
}
const handleCurrentChange = (val) => {
    page.value.pageNum = val
    getData()
}
//重置查询条件
const reset = () => {
    form.value = {}
    getData()
}
const handleSelectionChange = (val) => {
    let idArr = val.map(item => item.id)
    ids.value = idArr
}
//新增或修改
const toDetail = (id) => {
    detail.value.init(id)
}

//删除
const deleteData = (id) => {
    let params
    if (id !== 'batch') {
        params = [id]
    } else {
        if (ids.value.length === 0) {
            ElMessage.warning('请先选择需要删除的行，再做操作!')
            return
        }
        params = ids.value
    }
    api.post('/system/errorLogs/delete', params).then(res => {
        if (res.data.code === 200) {
            ElMessage.success(res.data.msg)
            getData()
        } else {
            ElMessage.error(res.data.msg)
        }
    })
}

onMounted(() => {
    getData()
})
</script>
<style scoped lang="less"></style>
